//
//  BusinessCertificationStep1Form.swift
//  iModern
//
//  Created by Ralf on 8/3/15.
//  Copyright (c) 2015 com.starlooking. All rights reserved.
//

import UIKit
import XLForm

class BusinessCertificationStep1Form: FormViewController
{
    var contactProfile = NSMutableDictionary()
    override func viewDidLoad()
    {
        super.viewDidLoad()
        self.navigationItem.title = "开设匠铺"
        self.initializeForm()
        self.initBackButton()
    }

    override func viewWillAppear(animated: Bool)
    {
        super.viewWillAppear(animated);
        MobClick.beginLogPageView("BusinessCertificationView1")

    }

    override func viewWillDisappear(animated: Bool)
    {
        super.viewWillDisappear(animated);
        MobClick.endLogPageView("BusinessCertificationView1")
    }

    override func didReceiveMemoryWarning()
    {
        super.didReceiveMemoryWarning()
    }

    func initializeForm()
    {
        var form:    XLFormDescriptor
        var section: XLFormSectionDescriptor
        var row:     XLFormRowDescriptor

        form = XLFormDescriptor(title: "")

        section = XLFormSectionDescriptor.formSectionWithTitle("填写您的匠铺名称")
        form.addFormSection(section)

        row = XLFormRowDescriptor(tag: "shopName", rowType: XLFormRowDescriptorTypeText, title: "匠铺名称：")
        row.cellConfigAtConfigure["textField.placeholder"] = "请填写您的匠铺名称"
        row.required = true
        section.addFormRow(row)

        row = XLFormRowDescriptor(tag: "next", rowType: XLFormRowDescriptorTypeButton, title: "下一步")
        row.cellConfigAtConfigure["backgroundColor"] = Theme.theme_color
        row.cellConfig["textLabel.color"] = UIColor.whiteColor()
        row.action.formSelector = "nextTouchButton:"
        row.required = true
        section.addFormRow(row)

        self.form = form
    }

    func nextTouchButton(sender: XLFormRowDescriptor)
    {
        let shopName = self.formValues()["shopName"] as? String
        if shopName == nil
        {
            self.alertWarningMessage("请填写您的匠铺名称")
            return
        }

        let rs = appDelegate.contactManager.verifyShopName(shopName)
        print(rs)
        if rs == nil
        {
            self.alertNetworkConnectionException()
        }

        if rs!.hasErrors()
        {
            if rs?.errorCode == 3020
            {
                alertUserStatusException()
                NSNotificationCenter.defaultCenter().postNotificationName("Logout", object: nil)
                self.view.endEditing(true)
                navigationController?.popToRootViewControllerAnimated(true)
            }
            alertErrorMessage(rs!.errorMessage)
            return
        }

        self.contactProfile["shopName"] = self.formValues()["shopName"]
        let BusinessVC = BusinessCertificationStep2Form()
        BusinessVC.contactProfile = self.contactProfile
        self.pushViewController(BusinessVC)
    }
}
